package com.foruo.learn.designmode.singleton;

/**
 * 单例 - 饿汉式（即类被加载时实例化对象）
 * @author GaoYuan
 * @date 2018/5/4 上午8:09
 */
public class SingletonDemoEntity {
    /** 注意 static */
    private static SingletonDemoEntity singletonDemoEntity = new SingletonDemoEntity();

    /** 注意 private */
    private SingletonDemoEntity(){}

    public static SingletonDemoEntity getInstance(){
        return singletonDemoEntity;
    }


    public static void main(String[] args){
        MyThread myThread = new MyThread();

        Thread thread1 = new Thread(myThread);
        Thread thread2 = new Thread(myThread);
        Thread thread3 = new Thread(myThread);

        thread1.start();
        thread2.start();
        thread3.start();

        //此处可见三次打印相同内容，则表示是同一个对象
    }
}


class MyThread extends Thread{

    @Override
    public void run() {
        System.out.println(SingletonDemoEntity.getInstance().hashCode());
    }

}